package kfm.core;

import kfm.model.*;
import kfm.util.List;
import kfm.model.Admin;
import kfm.model.Vip;

import java.util.ArrayList;
import java.util.Scanner;

public class Store {
//    private static List adminList;
    private static ArrayList<Admin> adminList;

//    private static List vipList;
    private static ArrayList<Vip> vipList;

//    private static List goodsList;
    private static ArrayList<Goods> goodsList;
//    private static List cateGoryList;
    private static ArrayList<CateGory> cateGoryList;

//    private static List shopList;
    private static ArrayList<Shop> shopList;
//    private static List levellist;
      private static ArrayList<Level> levellist;
    private static final Scanner INPUT = new Scanner(System.in);
    private Account user;

    public Store() {

        //会员等级
        Level level = new Level("普通用户", 0, 1);
        levellist.add(level);
        Level level1 = new Level("银卡会员", 1000, 0.98);
        levellist.add(level1);
        Level level2 = new Level("金卡用户", 15000, 0.85);
        levellist.add(level2);
        Level level3 = new Level("白金卡用户", 20000, 0.8);
        levellist.add(level3);

        // 模拟已有数据
        Admin admin = new Admin("管理员", "123", "123");
        adminList.add(admin);

        // 模拟 会员账号
        Vip vip = new Vip("张三", "111", "111", (Level) levellist.get(0));
        vipList.add(vip);

        vip = new Vip("李四", "222", "222", (Level) levellist.get(1));
        vipList.add(vip);

        //分类模拟
        CateGory cateGory = new CateGory(1, "零食");
        cateGoryList.add(cateGory);
        CateGory cateGory1 = new CateGory(2, "蔬菜");
        cateGoryList.add(cateGory1);
        CateGory cateGory2 = new CateGory(3, "水果");
        cateGoryList.add(cateGory2);
        CateGory cateGory3 = new CateGory(4, "酒水");
        cateGoryList.add(cateGory3);
        CateGory cateGory4 = new CateGory(5, "玩具");
        cateGoryList.add(cateGory4);

        //商品模拟
        Goods goods = new Goods(1, "苹果", 2, 5, 20, (CateGory) cateGoryList.get(2));
        goodsList.add(goods);
        Goods goods1 = new Goods(2, "白菜", 1, 2, 50, (CateGory) cateGoryList.get(1));
        goodsList.add(goods1);
        Goods goods2 = new Goods(3, "薯片", 2, 5, 20, (CateGory) cateGoryList.get(0));
        goodsList.add(goods2);


    }


    public void open() {
        welcome();
        indexMenuList();
    }

    static {
        vipList = new ArrayList<Vip>(2);
        adminList = new ArrayList<Admin>(1);
        cateGoryList = new ArrayList<CateGory>(2);
        goodsList = new ArrayList<Goods>(2);
        shopList = new ArrayList<Shop>(1);
        levellist = new ArrayList<Level>(5);
    }

    /**
     * 首页
     */
    private void welcome() {
        println("================================");
        println("==                            ==");
        println("==    欢迎观临!  开发喵Store ~   ==");
        println("==                            ==");
        println("================================");
    }

    /**
     * 功能展示
     *
     * @param
     */
    private void indexMenuList() {
        println("1. vip登录");
        println("2. 管理员登录");
        println("3. 注册");
        println("0. 退出");
//        println("6. 展示会员");
        System.out.print("请选择以上功能序号为：");
        int input = INPUT.nextInt();

        jamp(input);
    }


    /**
     * 跳转
     *
     * @param
     */

    private void jamp(int in) {
        switch (in) {
            case 1 -> vipLogin();
            case 2 -> managerLogin();
            case 3 -> register();
            case 0 -> exit();
            case 101 -> shopping();
            case 102 -> shopcar();
            case 201 -> showVip();
            case 202 -> addGoods();
            case 203 -> addCateGory();
            case 204 -> {
                showCateGory();
                showGoodList();
                managerMenu();
            }
            case 205 -> indexMenuList();
            default -> {
                printErr("输入错误 请重新输入");
                indexMenuList();
                jamp(INPUT.nextInt());
            }
        }
    }


    /**
     * vip
     */
    private void vipMenu() {
        println("1. 购物");
        println("2. 查看购物车");
        print("请选择：");
        int input = INPUT.nextInt();
        out(input);

        if (input == 1 || input == 2) {
            input = input + (input == 0 ? 0 : 100);
            jamp(input);
        } else {
            println("输入错误！请重新选择！");
        }
    }

    private void shopping() {
        showGoodList();
        if (goodsList != null) {
            println("请输入选择商品序号：");
            int input = INPUT.nextInt();
            out(input);
//            int index = goodsList.search(input);
//            int index = goodsList.indexOf(new Goods(input));
//            Goods goods = goodsList.get(index);

            int i;
            for (i = 0; i < goodsList.size(); i++) {
                Object element = goodsList.get(i);
                Goods goods = (Goods) element;
                if (goods.getGoodNo() == input) {

                    println("请输入购买 【" + goods.getGoodsName() + "】的数量为  ");
                    int count = INPUT.nextInt();
                    out(count);
                    while (goods.getCount() < count) {
                        println("库存不足，请重新输入！");
                        count = INPUT.nextInt();
                    }
                    shopList.add(new Shop(count, goods));
                    break;
                }
            }
            if (i == goodsList.size()) {
                printErr("暂无此商品！！ ");
                vipMenu();
            }
        }
        down();
        vipMenu();
    }

    private void shopcar() {
        println(shopList.toString());
        int in;
        println("是否继续购物？");
        print("1、 继续选购  2、结账");
        in = INPUT.nextInt();
        out(in);
        if (in == 1) {
            shopping();
        } else if (in == 2) {
//                print("结账");
                pay();
        } else {
            printErr("输入错误，请重新输入");
            shopcar();
        }
    }

    private void pay() {
        double money = 0;
        println("【您的账单如下】");
        println(shopList.toString());
        for (int i = 0; i < shopList.size(); i++) {
            Shop element = shopList.get(i);
            money += element.getShopnum() * element.getShopgoods().getPrice();

        }


        if (user instanceof Vip vip) {
            Level level = vip.getLevel();
            level.getName();
            if (!level.getName().equals("普通用户")) {
                print("尊敬的" + level.getName() + "您可享" + level.getDiscount() + "折优惠");
                println("原价为：" + money + "元");
                money = money * level.getDiscount();
            }
            println("应收：" + money + "元");
            do {
                println("所给金额：");
                double input = INPUT.nextDouble();
                if (input == 0) {
                    exit();
                }
                if (input >= money) {
                    println("实收：" + input + "元");
                    println("找零：" + String.format("%f",(input - money))+ "元");
                    break;
                }
                printErr("金额不足！重新给 = . = !");
            } while (true);
            vip.getPoint();
            vip.setPoint(vip.getPoint() + (int) money);

            for (int i = levellist.size() - 1; i > 0; i--) {
                Level element = (Level) levellist.get(i);
                if (vip.getPoint() >= element.getBase()) {
                    if (!vip.getLevel().getName().equals(element.getName())) {
                        println("恭喜您升级为" + element.getName() + "下次购物可享" + element.getDiscount() + "折");
                        break;
                    }
                }

                if (vip.getLevel().getName().equals("普通用户")) {
                    println("您距离银卡会员还差 ：" + (1000 - money) + "积分");
                    break;
                }
            }
//            shopList = new List(1);
            shopList = new ArrayList<>(1);
            shopcar();
            exit();
            indexMenuList();
        }
    }

    private void down() {
        for (int i = 0; i < shopList.size(); i++) {
            Shop shopcar = shopList.get(i);
            Goods goods = shopcar.getShopgoods();

            goods.setCount(goods.getCount() - shopcar.getShopnum());
        }
    }

    /**
     * admin
     */
    private void showCateGory() {
        String category = cateGoryList.toString();
        println("【商品分类】");
        if ("".equals(category)) {
            println("暂无商品分类");
        } else {
            println(category);
        }
    }

    private void showGoodList() {
        if (user instanceof Admin) {
            print(goodsList.toString());
        }
        if (user instanceof Vip) {
            for (int i = 0; i < goodsList.size(); i++) {
                Goods element = goodsList.get(i);
                println(element.toShow());
            }

        }
    }

    private void addCateGory() {
        showCateGory();
        println("请添加分类编号");
        int no = INPUT.nextInt();
        out(no);
        println("请添加分类名称");
        String name = INPUT.next();
        out(name);
        CateGory cateGory = new CateGory(no, name);
        cateGoryList.add(cateGory);

        managerMenu();

    }

    private void addGoods() {
        println("请输入商品编号：");
        int no = INPUT.nextInt();
        out(no);


        Goods goods = null;

        for (int i = 0; i < goodsList.size(); i++) {
            Object element = goodsList.get(i);

            if (element instanceof Goods goods1) {
                if (goods1.getGoodNo() == no) {
                    goods = goods1;
                }
            }
        }

        if (goods == null) {
            goods = new Goods();
            goods.setGoodNo(no);

            println("请输入商品名称");
            String name = INPUT.next();
            out(name);
            goods.setGoodsName(name);

            println("请输入商品进价");
            double core = INPUT.nextDouble();
            out(core);
            goods.setCost(core);

            println("请输入商品售价");
            double price = INPUT.nextDouble();
            out(price);
            goods.setPrice(price);

            showCateGory();
            println("请选择商品分类");
            int category = INPUT.nextInt();
            out(category);
            for (int i = 0; i < cateGoryList.size(); i++) {
                Object element = cateGoryList.get(i);
                if (element instanceof CateGory category1) {
                    if (category1.getNo() == category) {
                        goods.setCateGory(category1);
                        goodsList.add(goods);
                        break;
                    }
                }
            }
        }

        println("请输入商品数量");
        int count = INPUT.nextInt();
        out(count);
        goods.setCount(goods.getCount() + count);


        println("添加商品成功");

        managerMenu();

    }

    private void vipLogin() {
        println("请根据提示登录信息 ~ ");
        println("请输入VIP手机号：");
        String phone = INPUT.next();
        out(phone);
        println("请输入VIP密码：");
        String passWord = INPUT.next();
        out(passWord);
        Vip login = new Vip(null, phone, passWord);
        Account flag = checkAccount(login, vipList);
        if (flag instanceof Vip vip) {
            login = vip;
        }
        if (flag != null) {
            user = login;
            println(login.getName() + "  欢迎登录！");
            vipMenu();
        } else {
            printErr("账号或密码错误，请重试！");
            indexMenuList();
        }
    }


    private Account checkAccount(Account account, ArrayList list) {
        for (int i = 0; i < list.size(); i++) {
            Object element = list.get(i);
            if (element instanceof Account account1) {
                if (account.login(account1)) {
                    return account1;
                }
            }
        }
        return null;
    }

    private void managerLogin() {
        println("请根据提示管理员登录信息 ~ ");

        println("请输入管理员手机号：");
        String phone = INPUT.next();
        out(phone);


        println("请输入管理员密码：");
        String passWord = INPUT.next();
        out(passWord);
        Admin adminLogin = new Admin(null, phone, passWord);
        Account flag = checkAccount(adminLogin, adminList);
        if (flag instanceof Admin admin) {
            adminLogin = admin;
        }
        if (flag != null) {
            user = adminLogin;
            println(adminLogin.getName() + "欢迎登录！");
            managerMenu();
        } else {
            printErr("账号或密码错误，请重试！");
            indexMenuList();
        }
    }

    private void managerMenu() {

        println("1. 显示会员信息");
        println("2. 商品入库");
        println("3. 新增商品分类");
        println("4. 商品列表");
        println("5. 返回登录页");
        println("0. 退出");
        println("请选择以上功能：");
        int input = INPUT.nextInt();
        input = input + (input == 0 ? 0 : 200);
        jamp(input);
    }

    private void register() {
        println("请根据提示输入注册信息：");
        println("请输入注册电话：");
        String phone = INPUT.next();
        out(phone);
        for (int i = 0; i < vipList.size(); i++) {
            Object element = vipList.get(i);
            if (element instanceof Vip vip) {
                if (phone.equals(vip.getPhone())) {
                    printErr("该手机号已被注册，请重新输入!!!");
                    println("");
                    register();
                    break;
                }
            }
        }
        println("请输入姓名：");
        String name = INPUT.next();
        out(name);
        String passWord = null;
        do {
            println("请输入注册密码：");
            passWord = INPUT.next();
            out(passWord);
            println("请确认输入的注册密码：");
            String passWord2 = INPUT.next();
            out(passWord2);
            if (!passWord2.equals(passWord)) {
                println("两次密码不同，请重新输入密码");
            } else {
                break;
            }
        } while (true);
        Level level = new Level("普通用户", 0, 1);
        Vip vip = new Vip(name, phone, passWord, level);
        addVip(vip);
        println(name + "恭喜您成为本店会员！");


        indexMenuList();
    }

    private void exit() {
        println("欢迎下次光临 ~ ");
    }

    public static boolean addVip(Vip vip) {
        return vipList.add(vip);
    }

    /**
     * 显示会员信息
     */

    private void showVip() {

        println("现在共有" + vipList.size() + "个会员");
        println(vipList.toString());
        println("现在共有" + adminList.size() + "个管理员");
        println(adminList.toString());
        managerMenu();
    }



    public void println(String str) {
        System.out.println(str);
    }

    
    public void print(String str) {
        System.out.print(str);
    }

    public void printErr(String str) {
        System.err.println(str);
    }



    public void out(Object obj) {
        if (obj.equals("0") ||obj.equals(0)) {
            exit();
            indexMenuList();
        }
    }

}
